$(function(){
    show_total_count()
    get_local_items("../javascript/items.json", show_item_list,bind_event_add_in_cart)
    bind_event_cart()
})
function bind_event_cart(){
    $("#cart").click(function(){
    window.location.href='../html/cart_list.html';})
}
function get_local_items(url, done,add_in){
    $.getJSON(url,function(item_list){
        done(item_list)
        add_in(item_list)
    })
}
function get_local_promotion_items(url, done){
    $.getJSON(url,function(item_list){
        done(item_list)
    })
}
function save_in_localStorage(key,value){
    localStorage.setItem(key,value)
}
function get_in_localStorage(key){
     return localStorage.getItem(key)
}
function settle_total_count(){
    var total_count=$("#goods_count").html();
    total_count++;
    return total_count
}
function save_total_count(total_count){
    save_in_localStorage("item_total_count",total_count);
}
function show_total_count() {
    var total_count=get_total_count();
    $("#goods_count").html(total_count);
}
function get_total_count(){
   return get_in_localStorage("item_total_count")||0;
}
function show_item_list(item_list){
        $('#model').tmpl(item_list).appendTo('#table_head');
}
function get_cart_info(){
    return JSON.parse(localStorage.getItem("items_in_cart"))||[];
}
function is_in_cart(items_in_cart,id){
    return _.find(items_in_cart,function(item){
        return id === item.id;
    })
}
function update_total_count(){
    var total_count=settle_total_count();
    save_total_count(total_count)
}
function bind_event_add_in_cart(item_list){

        $(".add_in_cart").click(function(){
            var id= this.id;
            update_cart_list(item_list,id)
            var total_count=update_total_count()
            show_total_count(total_count)
        })
}
function update_cart_list(item_list,id){
    var items_in_cart=get_cart_info();
    var current_item=is_in_cart(items_in_cart,id);
     update_current_item(current_item,id,items_in_cart,item_list);
    save_in_localStorage("items_in_cart",JSON.stringify(items_in_cart))
}
function update_current_item(current_item,id,items_in_cart,item_list){
        if(current_item == undefined){
            add_current_item_in_cart(id,items_in_cart,item_list)
        }else{
            update_current_item_count(current_item)
        }
    get_local_promotion_items("../javascript/promotion_items.json", get_free_count)
}
function add_current_item_in_cart(id,items_in_cart,item_list){
    var info=_.find(item_list,function(item){
        return id === item.id
    })
    info.count=1;
    items_in_cart.push(info);
}
function update_current_item_count(current_item){
    current_item.count++;
}
function get_free_count(info){
        var items_in_cart = get_cart_info();
    settle_all_free_count(info,items_in_cart)
    save_in_localStorage("items_in_cart",JSON.stringify(items_in_cart));
}
function settle_all_free_count(info,items_in_cart){
    _.each(items_in_cart,function(item){
        settle_free_count(info,item)
    })
}

function settle_free_count(info,item){
    item.free_count=0;
    if (((info[0].id).indexOf(item.id) >= 0) &&  Math.floor((item.count) / 3) > 0) {
        item.free_count=Math.floor((item.count) / 3);
    }
}


